<template>
	<view>
		<notices :messages="messages" :handleNoticeInfo="handleNoticeInfo">
		</notices>
	</view>
</template>

<script>
import textMap from "@/utils/TextMap.js"
import c from "@/utils/Constant.js"
import noticeParser from "@/utils/NoticeParser.js"
import notices from "@/pages/message/notices.vue";
import messageApi from "@/request/api/messageApi.js";
import communityApi from "@/request/api/communityApi.js";
import listUtils from "@/utils/ListUtils.js"
import userUtils from "@/utils/userUtils.js"
import noticeBuilder from "@/utils/NoticeBuilder.js"
const commentUrl = "#/"  //评论详情的url
const topicUrl = "#/"	 //话题详情的url
	export default {
		data() {
			return {
				messages:[	
				],
				comments:[], //评论集合
				topics:[],    //话题集合
				page:{
					currentPage:1,
					size:10
				},
				loginUser:uni.getStorageSync('user')
			}
		},
		created(){
			//拉取系统发给当前用户的消息
			//拉取消息里相关的所有实体（评论、文章等等）
			// console.log(communityApi.batchTopicInfo(["1374953173827366913","1374901162507464705","1375238289178980354","1375242658599366657"])) 
			// console.log(communityApi.batchCommentInfo(["5465464981","5465464988","5646787774"]))
			
			this.getReplyMeessage();

		},
		methods: {
			//拉取回复用户的消息
			getReplyMeessage(){
				messageApi.historyListByType(c.action.REPLY,this.page).then((data)=>{
					if(data.code ==0){
						this.getEntitys(data.page.list)
						setTimeout(()=>{
							this.messages = [...this.messages,...data.page.list]
						},50);
					}
					
				})
			},
			//拉取实体
			getEntitys(messages){
				let commentsIds = []
				let topicIds = []
				for(let message of messages){
					
					let msgCon = JSON.parse(message.content);
					if(msgCon.entityId){//有实体id。才进行拉取
						if(msgCon.entityType == c.entityType.TOPIC){
							topicIds.push(msgCon.entityId)
						}else if(msgCon.entityType == c.entityType.COMMENT){
							commentsIds.push(msgCon.entityId)
						}
					}
				}
				//拉取多个话题
				communityApi.batchTopicInfo(listUtils.removeRepeat(topicIds)).then((data)=>{
					if(data.code == 0 && data.data){
						this.topics = [...this.topics,...data.data]
					}
				})
				
				//拉取多个评论
				communityApi.batchCommentInfo(listUtils.removeRepeat(commentsIds)).then((data)=>{
					if(data.code == 0 && data.data){
						this.comments = [...this.comments,...data.data]
					}
				})
			},
			
			
			//处理通知的逻辑，有通知组件来调用
			handleNoticeInfo(message,trigger){
				let msgCon = JSON.parse(message.content);
				if(msgCon.entityType == c.entityType.TOPIC ){//实体类型为话题
					return this.buildTopicNotice(message,trigger)
				}else if(msgCon.entityType == c.entityType.COMMENT ){//实体类型为评论
					return this.buildCommentNotice(message,trigger)
				}
			},
			
			//构建实体类型为主题的通知对象
			buildTopicNotice(message,trigger){
				//根据事件触发者的id，找出回复我的评论
				console.log(trigger)
				console.log(this.comments)
				let comment = listUtils.findElem(this.comments,"userId",trigger.id)
				
				//通过解析器获取消息对应的通知内容
				let title = noticeParser.parseTitle(message,trigger.nickname);
				//构建通知对象
				return noticeBuilder.buildTopicNotice(message,this.topics,title,comment.replyText)
			},
			//构建实体类型为评论的系统通知对象
			buildCommentNotice(message,trigger){
				//根据事件触发者的id,找出回复我的评论
				let comment = listUtils.findElem(this.comments,"userId",trigger.id)
				//通过解析器获取消息对应的通知内容
				let title = noticeParser.parseTitle(message,trigger.nickname);
				//构建通知对象
				return noticeBuilder.buildCommentNotice(message,this.comments,title,comment.replyText)	
			}
		},
		components:{
			notices
		},
		// 页面上拉触底事件的处理函数
		onReachBottom: function() {
			// if (!this.pullUpOn) return;
			// this.loadding = true
			// if (this.pageIndex == 3) {
			// 	this.loadding = false;
			// 	this.pullUpOn = false
			// } else {
			// 	let arr = JSON.parse(JSON.stringify(this.cmtList));
			// 	this.cmtList = this.cmtList.concat(arr);
			// 	this.pageIndex = this.pageIndex + 1;
			// 	this.loadding = false
			// }
		}
	}
</script>


<style>
</style>
